<template>
  <el-dialog
    :fullscreen="true"
    :visible.sync="param.visible"
    title="修改栏目"
    center
    @close="param.close"
    :close-on-click-modal="false"
  >
    <Form1
      ref="editForm"
      :rules="rules"
      :form="editForm"
      :items="items"
      :save="doUpdate"
    >
    </Form1>
  </el-dialog>
</template>
<script>
import Form1 from "@/components/Form1.vue";

export default {
  name: "Edit",
  components: { Form1 },

  props: {
    param: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      editForm: {
        id: "",
        name: "",
        parent_id: null,
        channel_img: "",
        summary: "",
        single: false,
        url: "",
        content: "",
        sort: "",
        postion: "",
      },
      //验证规则
      rules: {
        userName: [{ required: true, message: "用户名不能为空" }],
        name: [{ required: true, message: "姓名不能为空" }],
        password: [{ required: true, message: "密码不能为空" }],
      },
      items: [
        {
          type: "text",
          label: "栏目名称",
          prop: "name",
          name: "name",
          placeholder: "请输入栏目名称",
        },
        {
          type: "treeselect",
          label: "上级栏目",
          prop: "parent_id",
          name: "parent_id",
          placeholder: "请输选择上级栏目",
          options: [],
        },
        {
          type: "file",
          label: "标题图",
          prop: "channel_img",
          name: "channel_img",
          placeholder: "上传图片",
        },
        {
          type: "text",
          label: "摘要",
          prop: "summary",
          name: "summary",
          placeholder: "请输入摘要",
        },
        {
          type: "switch",
          label: "单页",
          prop: "single",
          name: "single",
          placeholder: "请选择单页",
        },
        {
          type: "text",
          label: "外链",
          prop: "url",
          name: "url",
          placeholder: "请输入外链",
        },
        {
          type: "number",
          label: "排序号",
          prop: "sort",
          name: "sort",
          placeholder: "请输入排序号",
        },
        {
          type: "number",
          label: "位置",
          prop: "postion",
          name: "postion",
          placeholder: "请输入位置",
        },
        {
          type: "area",
          label: "内容",
          prop: "content",
          name: "content",
          placeholder: "内容...",
        },
      ],
    };
  },
  mounted() {
    this.editForm = JSON.parse(JSON.stringify(this.param.editForm)); //避免俩对象指向同一个地址
    if (this.editForm.single == "Y") {
      this.editForm.single = true;
    } else {
      this.editForm.single = false;
    }
    this.tree();
  },
  methods: {
    //树形
    tree() {
      this.getRequest("/channel/tree").then((res) => {
        if (res) {
          console.log(res.obj, "树形list。。");
          this.items.forEach((item) => {
            if (item.type == "treeselect" && item.name == "parent_id") {
              item.options = res.obj;
            }
          });
        }
      });
    },
    //   修改
    doUpdate() {
      let flag = this.$refs["editForm"].validateForm();
      if (flag) {
        let param = this.editForm;
        if (this.editForm.single) {
          param.single = "Y";
        } else {
          param.single = "N";
        }

        //   保存
        this.putRequest("/channel/", param).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            this.param.close(); //关闭
            this.param.callback(); //重新查询
          }
        });
      }
    },
  },
};
</script>
<style></style>
